package com.longteng.diamond.service;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.longteng.diamond.dao.InterfaceMockDataMapper;
import com.longteng.diamond.dao.InterfaceMockMapper;
import com.longteng.diamond.domain.InterfaceMock;
import com.longteng.diamond.domain.InterfaceMockData;

import com.longteng.diamond.factory.MockMatchDataFactory;
import com.longteng.diamond.po.TablePage;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.web.util.WebUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator
 * DATE : 2018/6/28
 * TIME : 20:33
 * PROJECT : java-lesson
 * PACKAGE : com.longteng.lesson
 *
 * @author <a href="mailto:taki@qq.com">Administrator</a>
 */
@Slf4j
@Component
public class InterfaceMockService {
    @Autowired
    InterfaceMockMapper interfaceMockMapper;
    @Autowired
    InterfaceMockDataMapper interfaceMockDataMapper;
    @Autowired
    MockMatchDataFactory matchDataFactory;

    /**
     * 获取接口
     *
     * @param interfaceMock
     * @return interfaceMock
     */
    public TablePage getInterfaceMock(InterfaceMock interfaceMock) {
        TablePage tablePage = new TablePage();
        try {
            /**通过limit计算返回的数据**/
            List<InterfaceMock> interfaceMockList = interfaceMockMapper.getInterfaceMock(interfaceMock);
            tablePage.setRows(interfaceMockList);
            /**获取当前where条件的所有行数，如果你的where条件不变化的话，每次返回的所有行数都是一样的**/
            long total = interfaceMockMapper.queryAllRow(interfaceMock);
            tablePage.setTotal(total);
            /**interfaceMockList 和total 它们的where条件应该是一模一样的**/
        } catch (Exception e) {
            log.error("查询接口列表异常", e);
        }
        return tablePage;
    }

    /**
     * 新增接口
     *
     * @param interfaceMock
     * @return
     */
    public void addInterfaceMock(InterfaceMock interfaceMock) {
        interfaceMockMapper.addInterfaceMock(interfaceMock);
    }

    /**
     * 修改接口
     *
     * @param interfaceMock
     * @return void
     */
    public void updateInterfaceMock(InterfaceMock interfaceMock) {
        interfaceMockMapper.updateInterfaceMock(interfaceMock);
    }

    /**
     * 根据id获取接口
     *
     * @param interfaceId
     * @return interfaceMock
     */
    public InterfaceMock findInterfaceMockById(Integer interfaceId) {
        return interfaceMockMapper.findInterfaceMockById(interfaceId);
    }

    /**
     * 删除接口
     *
     * @param interfaceId
     * @return void
     */
    public void deleteInterfaceMockById(Integer interfaceId) {
        interfaceMockMapper.deleteInterfaceMockById(interfaceId);
    }

    /**
     * 判断请求是否是mock接口
     *
     * @param httpRequest
     * @return
     */
    public InterfaceMock isMock(HttpServletRequest httpRequest) {
        String url = httpRequest.getRequestURL().toString();
        String ip = httpRequest.getRemoteAddr();
        String method = httpRequest.getMethod();
        InterfaceMock interfaceMock = new InterfaceMock();
        interfaceMock.setInterfaceName(url);
        interfaceMock.setIp(ip);
        interfaceMock.setInterfaceMethod(method);
        return this.isMock(interfaceMock);
    }

    public InterfaceMock isMock(InterfaceMock interfaceMock) {
        InterfaceMock interfaceMockDb = interfaceMockMapper.getInterfaceMockMatch(interfaceMock);
        return interfaceMockDb;
    }

    /**
     * 获取mockData
     * @param httpRequest
     * @return
     */
    public String getMockData(HttpServletRequest httpRequest) {
        String serverName = httpRequest.getServerName();
        String path = (String) httpRequest.getAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE);
        String url = httpRequest.getRequestURL().toString();
        url = url.substring(0, url.indexOf(":") + 1);
        url = url + "//" + serverName + path;
        httpRequest.setAttribute("ordUrl",url);
        String ip = httpRequest.getRemoteAddr();
        String method = httpRequest.getMethod();
        InterfaceMock interfaceMock = new InterfaceMock();
        interfaceMock.setInterfaceName(url);
        interfaceMock.setIp(ip);
        interfaceMock.setInterfaceMethod(method);
        interfaceMock = this.isMock(interfaceMock);
        /**查询当前接口下有哪些mock数据**/
        Integer interfaceMockId = interfaceMock.getInterfaceMockId();
        String matchType = interfaceMock.getMatchType();
        List<InterfaceMockData> interfaceMockDataList = interfaceMockDataMapper.getMockDataByInterfaceMockId(interfaceMockId);
        if(CollectionUtils.isEmpty(interfaceMockDataList)){
            return "您的mock接口【"+interfaceMock.getInterfaceAliasName()+"】还没有配置mock返回数据,请先配置数据";
        }
        /**如果接口mock的匹配类型是无匹配,那么返回数据里面的第一条数据**/
        if(matchType.equalsIgnoreCase("noMatch")){
           return interfaceMockDataList.get(0).getMockData();
        }
        /**通过matchType去拿真正的逻辑处理类**/
        MockMatchDataService matchService= matchDataFactory.getMatchService(matchType);
        return matchService.dataMatch(interfaceMock,interfaceMockDataList,httpRequest);
    }
}
